<template>
  <!--
    回显用户信息
    1：导入获取员工详情api
    2:进入页面获取路由的id调用接口获取详情
    3：存储并渲染

    修改姓名与密码
    1:表单验证
    2：定义修改api
    3:导入修改api
    4:在点击确定在表单验证通过后调用api
    5:调用成功
       a:提示
       b:只要修改都要考虑更新数据

    基础信息
    1：定义基础信息获取api
    2:导入api
    3:进入页面调用基础信息api
    4:定义修改基础信息api
    5:导入基础信息修改api
    6:点击确定调用修改api
      提示

 -->

  <div class="detail">
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录帐户设置">
          <el-form ref="form" label-width="100px" class="form" :model="form" :rules="rules">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="新密码" prop="password">
              <el-input v-model="form.password" show-password />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" lazy>
          <Info :user-info="userInfo" @getData="getData" />
        </el-tab-pane>
        <el-tab-pane label="岗位信息" lazy><Job /></el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import { sysUser } from '@/api/user'
import { sysUserPut } from '@/api/employees'
// import Info from './components/Info.vue'
export default {
  components: {
    Info: () => import('./components/Info.vue'),
    Job: () => import('./components/Job .vue')
  },
  data() {
    return {
      userId: this.$route.params.id,
      form: {
        username: '', //	string	非必须
        password: '' //	string	非必须
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须
        password: [{ required: true, message: '必填', trigger: 'change' }, {
          min: 6, max: 12, message: '请输入6-12位密码', trigger: 'change'
        }] //	string	非必须
      },
      userInfo: {},
      userInfo2: {}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.userId)
      this.form.username = res.data.username
      this.userInfo = JSON.parse(JSON.stringify(res.data))
      // this.userInfo = res.data
      this.userInfo2 = res.data
      console.log('员工详情', res)
    },
    // 更新点击
    async  submit() {
      // validate支持promise,它的返回值就是promise
      // .then代表验证成功 .catch代表验证失败
      // this.$refs.form.validate((result) => {
      //   if (result) {
      //     console.log(result)
      //   }
      // })

      await this.$refs.form.validate()
      await sysUserPut({ ...this.userInfo2, ...this.form })
      this.$message.success('更新成功')
      if (this.userId === this.$store.state.user.userInfo.id) {
        this.$store.dispatch('user/getUserInfo')
      }
      // 更新当前数据
      this.getData()
      console.log('验证成功')
    }
  }
}
</script>
<style lang='scss' scoped>
.detail{
    padding:20px;
    .form{
        width:400px;
        margin:50px 0 0 150px;
    }
}
</style>
